package cn.sytton.taffecloud.service.system.service.impl;

import cn.sytton.taffecloud.common.api.service.RemoteAuthService;
import cn.sytton.taffecloud.common.base.exception.BizException;
import cn.sytton.taffecloud.common.base.response.R;
import cn.sytton.taffecloud.common.base.util.JsonUtil;
import cn.sytton.taffecloud.common.base.util.SecurityUtil;
import cn.sytton.taffecloud.common.db.model.PageParam;
import cn.sytton.taffecloud.common.servlet.service.BaseService;
import cn.sytton.taffecloud.service.system.entity.UserEntity;
import cn.sytton.taffecloud.service.system.mapper.UserMapper;
import cn.sytton.taffecloud.service.system.service.IUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户服务层
 *
 * @author skyrock
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService extends BaseService<UserEntity> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RemoteAuthService remoteAuthService;


    @Override
    public PageInfo<UserEntity> findListPage(PageParam pp) {
        return findListPage(pp, userMapper);
    }

    @Override
    public UserEntity findById(String id) {
        return userMapper.findById(id);
    }

    @Override
    public void create(UserEntity entity) {
        R<String> encodePwd = remoteAuthService.encode(entity.getPassword());
        if (encodePwd.isSuccess()) {
            userMapper.create(entity);
        } else {
            //异常
            log.error(JsonUtil.getString(encodePwd));
        }
    }

    @Override
    public void update(UserEntity entity) {
        userMapper.update(entity);
    }

    @Override
    public void delete(String... ids) {
        userMapper.delete(ids);
    }

    @Override
    public void updatePwd(UserEntity entity) {
        R<String> encodePwd = remoteAuthService.encode(entity.getPassword());
        if (encodePwd.isSuccess()) {
            userMapper.updatePwd(entity);
        } else {
            //异常
            log.error(JsonUtil.getString(encodePwd));
        }
    }

    @Override
    public void updateUserStatus(UserEntity entity) {
        userMapper.updateUserStatus(entity);
    }

    @Override
    public void updateMyPwd(String oldPwd, String newPwd) {
        UserEntity user = userMapper.findById(SecurityUtil.getUserId());
        if (user != null) {
            R<Boolean> matches = remoteAuthService.matches(oldPwd, user.getPassword());
            if (matches.isSuccess()) {
                if (matches.getData()) {
                    R<String> encodePwd = remoteAuthService.encode(newPwd);
                    if (encodePwd.isSuccess()) {
                        user.setPassword(encodePwd.getData());
                        user.updateField();
                        userMapper.updatePwd(user);
                    } else {
                        //异常
                        log.error(JsonUtil.getString(encodePwd));
                    }
                } else {
                    throw new BizException("旧密码错误");
                }
            } else {
                //异常
                log.error(JsonUtil.getString(matches));
            }
        } else {
            throw new BizException ("用户不存在");
        }
    }
}
